package com.mvp.common;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.mvp.utils.SharedPreferencesUtils;

import java.util.Locale;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2018/11/7.
 * 语言管理
 */
public class LocalManageUtil {

    private static LocalManageUtil instance;
    private Context mContext;

    public static void init(Context mContext) {
        if (instance == null) {
            synchronized (LocalManageUtil.class) {
                if (instance == null) {
                    instance = new LocalManageUtil(mContext);
                }
            }
        }
    }
    /**
     * @return en/cn
     */
    public static String getAppLanguage() {
        return LocalManageUtil.getAppLanguageSelect() == Locale.ENGLISH ? "en" : "cn";
    }
    public static LocalManageUtil getInstance() {
        if (instance == null) {
            throw new IllegalStateException("You must be init MultiLanguageUtil first");
        }
        return instance;
    }

    private LocalManageUtil(Context context) {
        this.mContext = context;
    }


    public static Context attachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationResources(context);
        } else {
            LocalManageUtil.getInstance().setConfiguration();
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context createConfigurationResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getInstance().getLanguageLocale();
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }

    /**
     * 设置语言
     */
    public void setConfiguration() {
        Locale targetLocale = getLanguageLocale();
        Configuration configuration = mContext.getResources().getConfiguration();
        configuration.setLocale(targetLocale);
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
    }

    private static final String APP_LANGUAGE_SELECT = "APP_LANGUAGE_SELECT";//已经选择的语言

    /**
     * 如果不是英文、简体中文、繁体中文，默认返回简体中文
     *
     * @return
     */
    private Locale getLanguageLocale() {
        return getAppLanguageSelect();
    }



    /**
     * 获取设置的语言
     * 0/1/2 系统默认/英文/中文
     *
     * @return
     */
    public static Locale getAppLanguageSelect() {
        int i = (int) SharedPreferencesUtils.getData(APP_LANGUAGE_SELECT,0);
        d("--->>>>>>>>---select.i:" + i);
        if (i != 1 && i != 2) {
            Locale locale = getSysLocale();
            d("--->>>>>>>>---系统默认语言:" + locale.getLanguage());
            if (!TextUtils.equals(locale.getLanguage(), "zh")) {
                d("--->>>>>>>>---APP默认语言不是中文,那么更改为英文");
                i = 1;
            } else {
                d("--->>>>>>>>---APP默认语言是中文");
                i = 2;
            }
        }
        if (i == 1) {//系统默认如果不是中文,那么就设置APP为英文
            return Locale.ENGLISH;
        } else {
            return Locale.CHINA;
        }
    }

    //以上获取方式需要特殊处理一下
    private static Locale getSysLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    /**
     * @param isEnglish 0/1/2 系统默认/英文/中文
     */
    public static void setAppLanguageSelect(boolean isEnglish) {
        SharedPreferencesUtils.putData(APP_LANGUAGE_SELECT, isEnglish ? 1 : 2);
    }
}
